//package com.yjk1.example.myFirstSofawebBoot;
//
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.beans.factory.config.BeanDefinition;
//import org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext;
//import org.springframework.context.annotation.AnnotationConfigApplicationContext;
//import org.springframework.context.annotation.ScannedGenericBeanDefinition;
//import org.springframework.core.io.Resource;
//import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
//import org.springframework.core.io.support.ResourcePatternResolver;
//import org.springframework.core.type.AnnotationMetadata;
//import org.springframework.core.type.classreading.MetadataReader;
//import org.springframework.core.type.classreading.MetadataReaderFactory;
//import org.springframework.core.type.classreading.SimpleMetadataReaderFactory;
//import org.springframework.stereotype.Component;
//import org.springframework.stereotype.Controller;
//import org.springframework.stereotype.Service;
//import org.springframework.util.ReflectionUtils;
//import org.springframework.web.bind.annotation.RequestMapping;
//import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
//import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
//
//import java.io.File;
//import java.lang.reflect.Method;
//import java.lang.reflect.Modifier;
//import java.net.MalformedURLException;
//import java.net.URL;
//import java.net.URLClassLoader;
//import java.util.ArrayList;
//import java.util.LinkedHashSet;
//import java.util.List;
//import java.util.Set;
//
//@Service
//public class LoadServiceSepContext {
//
//    @Autowired
//    private AnnotationConfigServletWebServerApplicationContext annotationConfigServletWebServerApplicationContext;
//
//    public void register(File file) {
//        try {
//            String jarPath = "E:\\open-source-prj\\PluginBizTest\\target\\PluginBizTest-1.0-SNAPSHOT.jar";
//            file = new File(jarPath);
//            //查找依赖的jar包，同级目录下的lib/
//            List<URL> dependencyJar = findDependencyJar(file);
//
//            // AnnotationConfigServletWebServerApplicationContext tempApplicationContext = annotationConfigServletWebServerApplicationContext;
//
///*            AnnotationConfigServletWebServerApplicationContext tempApplicationContext = new AnnotationConfigServletWebServerApplicationContext();
//            tempApplicationContext.setParent(annotationConfigServletWebServerApplicationContext);
//            tempApplicationContext.refresh();*/
//
//
//            URL[] urls = dependencyJar.toArray(new URL[dependencyJar.size()]);
//            //新建classloader 核心
//            URLClassLoader urlClassLoader = new URLClassLoader(urls,
//                    annotationConfigServletWebServerApplicationContext.getClassLoader());
//
//            //获取导入的jar的controller  service  dao 等类，并且创建BeanDefinition
//            //Set<BeanDefinition> beanDefinitions = getBeanDefinitions(urlClassLoader);
//
//            AnnotationConfigApplicationContext annotationConfigApplicationContext =
//                    new AnnotationConfigApplicationContext(annotationConfigServletWebServerApplicationContext.getDefaultListableBeanFactory());
//            annotationConfigApplicationContext.setParent(annotationConfigServletWebServerApplicationContext);
//            annotationConfigApplicationContext.getDefaultListableBeanFactory().setBeanClassLoader(urlClassLoader);
//
//            //annotationConfigApplicationContext.setClassLoader(urlClassLoader);
//            annotationConfigApplicationContext.refresh();
//        } catch (Exception ex) {
//            ex.printStackTrace();
//        }
//
//    }
//
//
//    private static List<URL> findDependencyJar(File file) throws MalformedURLException {
//        List<URL> list = new ArrayList<>();
//        File parentFile = file.getParentFile();
//        File libFile = new File(file.getParent() + File.separator + "lib");
//        if (libFile.exists() && parentFile.isDirectory()) {
//            for (File jar : libFile.listFiles()) {
//                if (jar.isFile()
//                        && jar.getName().toLowerCase().endsWith(".jar")
//                        ) {
//                    list.add(jar.toURI().toURL());
//                }
//            }
//        }
//        list.add(file.toURI().toURL());
//        return list;
//
//    }
//
//
//
//}
